import { Component, OnInit } from '@angular/core';
import {PageSubject} from "../../../../util/page-subject";
import {SiteAlertService} from "./site-alert.service";
import {ResponseUtil} from "../../../../util/response-util";
import {OneDialogService} from "../../../../widgets/one-dialog/one-dialog.service";
import {Path} from "../../../../portal/admin/admin-breadcrumb/breadcrumb";
import {TableUtil} from "../../../../util/table-util";
import {MsgResult} from "../../../../models/msg-result";
import {MsgConstants} from "../../../../constants/constants";

declare var $;
declare var moment;

@Component({
  selector: 'app-site-alert',
  templateUrl: './site-alert.component.html',
  styleUrls: ['./site-alert.component.css']
})
export class SiteAlertComponent implements OnInit {

  table : any;
  config  : any ;
  startTime : string = "";
  endTime : string = "";


  isLoading : boolean  = false;

  constructor(
    private pageSubject : PageSubject,
    private siteAlertService : SiteAlertService,
    private responseUtil :ResponseUtil,
    private tableUtil:TableUtil,
    private dialogService: OneDialogService,
  ) { }

  ngOnInit() {
    this.pushPage();
    this.initTime();
    this.loadTable();
    this.tableContentData();
  }
//面包屑导航
  pushPage() {
    let path = new Path("报警信息", "");
    this.pageSubject.pushPage("报警历史", "", path);
  }
  //初始化时间插件
  initTime(){
    this.startTime = moment().subtract(5,"h").format("YYYY-MM-DD HH:00:00");
    this.endTime = moment().format("YYYY-MM-DD HH:00:00");
    console.log(this.startTime);
    this.config = {
      timePicker:true,
      timePickerIncrement:5,
      timePicker24Hour:true,
      maxDate:moment().format("YYYY-MM-DD HH:00:00"),
      locale:{
        format:"YYYY-MM-DD HH:00",
        separator: " 至 ",
        applyLabel : '确定',
        cancelLabel : '取消',
        fromLabel: '从',
        toLabel: '到',
        weekLabel: 'W',
        customRangeLabel: 'Custom Range',
        daysOfWeek:["日","一","二","三","四","五","六"],
        monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
        firstDay:1
      },
      startDate: this.startTime,
      endDate: this.endTime
    };
    $("#siteAlertDate").daterangepicker(this.config);
  }
  //初始化表格
  loadTable(){
    this.table=this.tableUtil.load("siteAlertTable",{
      columns: [
        {
          field: 'index',
          title: '序号',
          valign:"middle",
          width:35
        },
        {
          field: 'alarmSiteName',
          title: '点位名称',
          width:90
        },
        {
          field: 'alarmTime',
          title: '监测时间',
          width:120
        },
        {
          field: 'createTime',
          title: '创建时间',
          width:120
        },
        {
          field: 'alarmInfo',
          title: '报警类型',
          width:80
        },
        {
          field:'alarmFactor',
          title: '污染物',
          width:100,
        },
        {
          field: 'alarmData',
          title: '超标数据',
          width:100,
        }
      ],
      data:[],
      striped :true,
      undefinedText : "-",
      height:$(window.innerHeight)[0]-224
    });
    console.log(this.table);
  }
  //表格数据
  tableContentData(){
    this.isLoading = true;
    console.log(this.startTime);
    console.log(this.endTime);
    let siteName = $("#siteAlertSite").val();
    this.siteAlertService.getWarnSiteDetail(this.startTime,this.endTime,siteName).subscribe(
      result => this.responseUtil.success(result,this.tableContentDataResult),
      message => this.responseUtil.error(message)
    );
  }
  private tableContentDataResult = (result: MsgResult) =>{
    this.isLoading = false;
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      if(result.data.length){
        $.each(result.data,(i,data)=>{
          result.data[i].index  = i+1;
          if(data.hasOwnProperty("alarmFactor")){
            result.data[i].alarmFactor = data.alarmFactor.join(",");
          }
          if(data.hasOwnProperty("alarmData")){
            result.data[i].alarmData = data.alarmData.join(",")
          }
        })
      }
      console.log(result.data);
      this.table.bootstrapTable("load",result.data);
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }
  /**
   * 查询
   */
  search(){
    let rangeTime = $("#siteAlertDate").val();
    this.startTime = $.trim(rangeTime.split("至")[0])+":00";
    this.endTime = $.trim(rangeTime.split("至")[1])+":00";
    this.tableContentData();
  }
}
